package com.example.dirviesystem.coach.controller;

import com.example.dirviesystem.coach.service.CoachInformService;
import com.example.dirviesystem.entity.CoachMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/coach-informs")
public class CoachInformController {

    @Autowired
    private CoachInformService coachInformService;

    @GetMapping("/{coachId}")
    public ResponseEntity<Page<CoachMessage>> getInformsByCoachId(
            @PathVariable Long coachId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {

        Page<CoachMessage> informs = coachInformService.findInformsByCoachId(
                coachId,
                startDate,
                endDate,
                PageRequest.of(page, size));

        return ResponseEntity.ok(informs);
    }

    @PutMapping("/{id}")
    public ResponseEntity<CoachMessage> updateInform(@PathVariable Long id) {
        CoachMessage inform = coachInformService.findById(id);
        if (inform != null) {
            inform.setLook(true); // 设置为已读
            coachInformService.saveInform(inform);
            return ResponseEntity.ok(inform);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    public CoachMessage createInform(@RequestBody CoachMessage inform) {
        return coachInformService.saveInform(inform);
    }
}